const router = require('koa-router')()
const koaBody = require('koa-body')();
const request = require('superagent'); //不要忘记npm install
var config=require('../config/app.config');
const co = require('co')

module.exports = router.get('/getWxCode/*',co.wrap(function*(ctx) {
    /**
     * 正常用户的微信授权
     * 微信授权登陆完成重定向 
     * 1.获取授权code
     * 2、带上当前商户或平台的编号请求后台
     * 3、获取到微信授权用户信息重定到path对应的路径
     */
    console.log(ctx.query.code)
    console.log(ctx.params.path)
    let path = ctx.params.path;
    var res = yield request
    .post(config.host+'/api/crm/weixin/getWxUser')
    .send({ customerId: '0', orgId: config.orgId, merchantId: config.merchantId, code: ctx.query.code })
    .set('X-API-Key', 'foobar')
    .set('Accept', 'application/json')
    .set('Content-Type', 'application/json');
    console.log('新生成的用户id');
    console.log(res.body.data.customerId);
    let id = res.body.data.customerId;
    ctx.response.redirect('http://qingwei.flyray.me/#/'+path+'?customerId='+id)
    //yield ctx.render('index', { customerId: '123875549725863936'});
}))